package com.liao.furns.service.impl;

import com.liao.furns.dao.FurnDAO;
import com.liao.furns.dao.impl.FurnDAOImpl;
import com.liao.furns.pojo.Furn;
import com.liao.furns.pojo.Page;
import com.liao.furns.service.FurnService;

import java.util.List;

/**
 * @author lzp
 * @version 1.0
 * lzpnb!
 */
public class FurnServiceImpl implements FurnService {
    private FurnDAO furnDAO= new FurnDAOImpl();
    @Override
    public List<Furn> queryFurns() {
        return furnDAO.queryFurns();
    }

    @Override
    public boolean add(Furn furn) {
        return furnDAO.saveFurn(furn) == 1 ? true : false;
    }

    @Override
    public int deleteFurnByid(Integer id) {
        return furnDAO.deleteFurn(id);
    }

    @Override
    public Furn queryFurnById(int id) {
        return furnDAO.queryFurnById(id);
    }

    @Override
    public int updateFurn(Furn furn) {
        return furnDAO.updateFurn(furn);
    }

    @Override
    public Page<Furn> page(int pageNo, int pageSize) {
        Page<Furn> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        int totalRow = furnDAO.getTotalRow();
        page.setTotalRow(totalRow);
        int pageTotalCount = totalRow / pageSize;
        if(totalRow % pageSize > 0){
            pageTotalCount++;
        }
        page.setPageTotalCount(pageTotalCount);
        int begin = (pageNo - 1 ) * pageSize;
        List<Furn> pageItems = furnDAO.getPageItems(begin, pageSize);
        page.setItems(pageItems);
        return page;
    }

    @Override
    public Page<Furn> pageByName(int pageNo, int pageSize, String name) {
        Page<Furn> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        int totalRow = furnDAO.getTotalRowByName(name);
        page.setTotalRow(totalRow);
        int pageTotalCount = totalRow / pageSize;
        if(totalRow % pageSize > 0){
            pageTotalCount++;
        }
        page.setPageTotalCount(pageTotalCount);
        int begin = (pageNo - 1 ) * pageSize;
        List<Furn> pageItems = furnDAO.getPageItemsByName(begin, pageSize,name);
        page.setItems(pageItems);
        return page;
    }
}
